package com.jinlejia.erp.ui.common;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.appcompat.app.AlertDialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.jinlejia.erp.R;

/**
 * 联系业主对话框
 */

public class ContactDialog extends AlertDialog {

    JSONObject data;

    public ContactDialog(Context context, JSONObject jsonObject) {
        super(context);
        this.data = jsonObject;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = this.getWindow();
        // *** 主要就是在这里实现这种效果的.
        // 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
        window.setContentView(R.layout.dialog_contact);
        window.setGravity(Gravity.BOTTOM);
        window.setBackgroundDrawable(new ColorDrawable());
        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        //楼层
        TextView floor = window.findViewById(R.id.tv_floor);
        //业主
        TextView owner = window.findViewById(R.id.tv_owner);
        //业主备用
        TextView ownerStandby = window.findViewById(R.id.tv_standby);
        //联系人
        TextView liankman = window.findViewById(R.id.tv_linkman);
        //查看数
        TextView checkCount = window.findViewById(R.id.check_count);

        floor.setText(data.getString("houseNumber") );
        owner.setText(data.getString("sourceOwner"));
        //业主备用电话
        if (data.containsKey("backuptel") && !TextUtils.isEmpty(data.getString("backuptel"))) {
            ownerStandby.setText(data.getString("sourceOwner"));
        } else {
            ownerStandby.setVisibility(View.GONE);
            window.findViewById(R.id.standby_tip).setVisibility(View.GONE);
        }
        //联系人
        if (data.containsKey("contact") && !TextUtils.isEmpty(data.getString("contact"))) {
            liankman.setText(data.getString("contact"));
        } else {
            liankman.setVisibility(View.GONE);
            window.findViewById(R.id.linkman_tip).setVisibility(View.GONE);
        }

        checkCount.setText("今日已查看" + data.getString("coreCount") + "/5次");
        if (callListener != null) {
            findViewById(R.id.tv_owner).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callListener.call(data.getString("tel"));
                }
            });
            findViewById(R.id.tv_standby).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callListener.call(data.getString("backuptel"));
                }
            });
            findViewById(R.id.tv_linkman).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callListener.call(data.getString("contacttel"));
                }
            });
        }
    }


    public interface OnCallListener {
        public void call(String tel);
    }

    private OnCallListener callListener;

    public void setOnCallListener(OnCallListener listener) {
        this.callListener = listener;
    }
}
